package com.d0318;

import java.sql.Time;
import java.util.concurrent.TimeUnit;
//同步方法要一个线程全部执行完才能执行下一个线程
public class Resource {
    public synchronized void f(){
        for (int i = 0; i < 5; i++) {
                               //先获取当前线程
            System.out.println(Thread.currentThread().getName()+"f() is running");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void g(){
        for (int i = 0; i < 5; i++) {
            //先获取当前线程
            System.out.println(Thread.currentThread().getName()+"g() is running");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void h(){
        for (int i = 0; i < 5; i++) {
            //先获取当前线程
            System.out.println(Thread.currentThread().getName()+"h() is running");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        Resource resource=new Resource();
        new Thread(()->resource.f()).start();
        new Thread(()->resource.g()).start();
        resource.h();

    }
}
